/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Shop_category                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： Shop_categoryService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class Shop_categoryService : IShop_categoryService
    {
        private readonly IShop_categoryRepository _repository;

        public Shop_categoryService(IShop_categoryRepository repository)
        {
            _repository = repository;
        }

        public int Add(Shop_category model)
        {
            model.id = (int)_repository.Insert(model);

            //查询父节点的深度赋值
            if (model.parent_id > 0)
            {
                Shop_category model2 = GetModel(model.parent_id);
                model.class_list = model2.class_list + model.id + ",";
                model.class_layer = model2.class_layer + 1;
            }
            else
            {
                model.class_list = "," + model.id + ",";
                model.class_layer = 1;
            }
            //修改节点列表和深度
            _repository.Update(model);
            return model.id;
        }

        public bool Delete(int id)
        {
            return _repository.Delete(id)>0;
        }

        public bool Exists(int id)
        {
            return GetModel(id) != null ? true : false;
        }

        public List<Shop_category> GetChildList(int top, int parent_id)
        {
            var list = _repository.GetList("where parent_id = " + parent_id);
            if (top > 0)
                list = list.Take(top);
            return list.ToList();
        }

        public List<Shop_category> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }

        public List<Shop_category> GetList(int parent_id)
        {
            string sqlWhere = "where id>0 order by sort_id asc,id desc";
            var list = _repository.GetList(sqlWhere).ToList();
            List<Shop_category> newlist = new List<Shop_category>();
            GetChilds(list, newlist, parent_id);
            return newlist;
        }

        public Shop_category GetModel(int id)
        {
            return _repository.Get(id);
        }

        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }

        public bool Update(Shop_category model)
        {
            //先判断选中的父节点是否被包含
            if (IsContainNode(model.id, model.parent_id))
            {
                //查找旧数据
                Models.Shop_category oldModel = GetModel(model.id);
                //查找旧父节点数据
                string class_list = "," + model.parent_id + ",";
                int class_layer = 1;
                if (oldModel.parent_id > 0)
                {
                    Models.Shop_category oldParentModel = GetModel(oldModel.parent_id);
                    class_list = oldParentModel.class_list + model.parent_id + ",";
                    class_layer = oldParentModel.class_layer + 1;
                }
                //先提升选中的父节点
                var modelp = GetModel(model.parent_id);
                modelp.parent_id = oldModel.parent_id;
                modelp.class_list = class_list;
                modelp.class_layer = class_layer;
                UpdateChilds(model.parent_id);//带事务
            }
            //更新子节点
            if (model.parent_id > 0)
            {
                Models.Shop_category model2 = GetModel(model.parent_id);
                model.class_list = model2.class_list + model.id + ",";
                model.class_layer = model2.class_layer + 1;
            }
            else
            {
                model.class_list = "," + model.id + ",";
                model.class_layer = 1;
            }
            _repository.Update(model);
            return true;
        }

        public bool UpdateField(int id, string strValue)
        {
            return _repository.UpdateField(id, strValue);
        }
        /// <summary>
        /// 从内存中取得所有下级类别列表（自身迭代）
        /// </summary>
        private void GetChilds(List<Models.Shop_category> oldData, List<Models.Shop_category> newData, int parent_id)
        {
            var dr = oldData.Where(a => a.parent_id == parent_id).ToList();
            for (int i = 0; i < dr.Count; i++)
            {
                newData.Add(dr[i]);
                //调用自身迭代
                this.GetChilds(oldData, newData, dr[i].id);
            }
        }
        /// <summary>
        /// 验证节点是否被包含
        /// </summary>
        /// <param name="id">待查询的节点</param>
        /// <param name="parent_id">父节点</param>
        /// <returns>bool</returns>
        private bool IsContainNode(int id, int parent_id)
        {
            return _repository.GetList(" where class_list like '%," + id + ",%' and id=" + parent_id).Count() > 0;
        }
        /// <summary>
        /// 修改子节点的ID列表及深度（自身迭代）
        /// </summary>
        private void UpdateChilds(int parent_id)
        {
            //查找父节点信息
            Models.Shop_category model = GetModel(parent_id);
            if (model != null)
            {
                //查找子节点
                var ds = _repository.GetList("where parent_id=" + parent_id).ToList();
                foreach (Shop_category dr in ds)
                {
                    //修改子节点的ID列表及深度
                    int id = dr.id;
                    string class_list = model.class_list + id + ",";
                    int class_layer = model.class_layer + 1;
                    var model2 = GetModel(id);
                    model2.class_list = class_list;
                    model2.class_layer = class_layer;
                    _repository.Update(model2);
                    //调用自身迭代
                    this.UpdateChilds(id);
                }
            }
        }
    }
}